import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../common/style/colors.dart';
import '../loading/loading_widget.dart';
import 'empty_widget.dart';
import 'over_scroll_behavior.dart';

class ZvRefreshWidget extends StatelessWidget {

  ///下拉刷新回调
  final VoidCallback? onRefresh;

  ///上拉加载回调
  final VoidCallback? onLoadMore;

  ///子类，必须是ListView
  final Widget child;
  final Widget? emptyWidget;
  final RefreshController controller;
  final int loadState;

  const ZvRefreshWidget(
      {Key? key,
      this.onRefresh,
      this.onLoadMore,
      required this.child,
      this.emptyWidget,
      required this.controller,
       this.loadState=1})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final refreshImg = Image.asset(
      "assets/images/refresh_idle.png",
      width: 46,
      height: 46,
    );
    return ScrollConfiguration(
        behavior: OverScrollBehavior(),
        child: SmartRefresher(
            controller: controller,
            enablePullDown: onRefresh == null ? false : true,
            enablePullUp: onLoadMore == null ? false : true,
            onRefresh: onRefresh,
            onLoading: onLoadMore,
            header: CustomHeader(
              builder: (BuildContext context, RefreshStatus? mode) {
                Widget header;
                // if (mode == RefreshStatus.idle) {
                //   ///下拉时显示
                //   header = refreshImg;
                // } else if (mode == RefreshStatus.refreshing) {
                //   ///加载中
                //   header = const SizedBox(width: 46, height: 46, child: LoadingWidget());
                // } else if (mode == RefreshStatus.failed) {
                //   ///加载失败
                //   header = refreshImg;
                // } else if (mode == RefreshStatus.completed) {
                //   header = refreshImg;
                // } else {
                //   header = refreshImg;
                // }
                return SizedBox(
                  height: 60,
                  child: Center(child: mode == RefreshStatus.refreshing ? const LoadingWidget() : refreshImg),
                );
              },
            ),
            footer: const ClassicFooter(
              loadingIcon: SizedBox(
                width: 18,
                height: 18,
                child: CupertinoActivityIndicator(
                  color: AppColors.color99,
                ),
              ),
              textStyle: TextStyle(color: AppColors.color33, fontSize: 12),
              idleText: "上拉加载更多",
              failedText: "加载失败了",
              noDataText: "没有更多数据了",
              loadingText: "加载中...",
              canLoadingText: "松开加载更多哦",
            ),
            child: (loadState == 1)
                ? child
                : loadState == 0
                    ? const SizedBox()
                    : loadState == 2
                        ? const EmptyWidget()
                        : const EmptyWidget(
                            message: "加载出错啦",
                          )));
  }
}
